import { Application } from "egg";
import * as AutoIncrementFactory from "mongoose-sequence";
import { ObjectId } from "mongoose";
export interface PrivilegesProps {
  id?: number;
  privileges_name: string;
  privileges_url: string;
  privileges_type_id: ObjectId;
  is_delete: boolean;
}
function initPrivilegesModel(app: Application) {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const AutoIncrement = AutoIncrementFactory(mongoose);
  const PrivilegesScheme = new Schema<PrivilegesProps>(
    {
      privileges_name: { type: String, unique: true, required: true },
      privileges_url: { type: String, unique: true, required: true },
      privileges_type_id: {
        type: Schema.Types.ObjectId,
        ref: "PrivilegesType",
        required: true,
      },
      is_delete: { type: Boolean, default: false },
    },
    {
      timestamps: true,
      toJSON: {
        transform(_doc, ret) {
          delete ret.password;
          delete ret.__v;
        },
      },
    }
  );
  PrivilegesScheme.plugin(AutoIncrement, {
    inc_field: "id",
    id: "Privileges_counter",
  });
  return app.mongoose.model<PrivilegesProps>("Privileges", PrivilegesScheme);
}

export default initPrivilegesModel;
